//
// Created by buyongjiu on 2024/4/20.
//

#include <iostream>
#include "../include/Grammar.h"



int Grammar::Scan_Regular_Grammar(std::vector<std::string> corresponding_input) {
    for(auto &element: corresponding_input) {
        Grammar_struct temp;
        auto t = element.find(" -> ");
        if(t == std::string::npos) {
            return -1;
        }
        temp.left = element.substr(0, t);
        std::string str = "";
        for(int i = t + 4; ; i++) {
            if(element[i] == '\0') {
                if(temp.right.first.empty()){
                    temp.right.first = str;
                    str = "";
                    break;
                }
                else {
                    temp.right.second = str;
                    str = "";
                    break;
                }
            }
            if(element[i] == ' ') {
                temp.right.first = str;
                str = "";
                continue;
            }
            str += element[i];
        }
        this->grammar.push_back(temp);
    }
    this->grammar_type = grammar[0].left;
    return 0;
}

void Grammar::Print_struct() {
    // 遍历 grammar 向量中的每个 Grammar_struct 对象
    for(const auto& gs : grammar) {
        // 打印 left 字符串
        std::cout << "Left: " << gs.left << std::endl;
        // 打印 right 中的 first 和 second 字符串
        std::cout << "Right - First: " << gs.right.first << ", Second: " << gs.right.second << std::endl;
        // 分隔符，可根据需要添加
        std::cout << "-----------------" << std::endl;
    }
}
